// queryengine.d: The query controller
// Author: Wei Li (oldrev@gmail.com)
// License: GPL

module dayudict.queryengine;

import dayudict.dict;
import dayudict.dictman;


/**
  * 建模查询控制器，是 MVC 模型中的 C，处理界面传来的查询请求，并返回结果
  * 在此类中实现查询规则，程序通过此类执行词典查询操作
  */
class QueryEngine
{
	invariant
	{
		assert(m_dictman !is null);
	}

	private DictionaryManager m_dictman = null;

	public this(DictionaryManager dm)
	in
	{
		assert(dm !is null);
	}
	body
	{
		m_dictman = dm;		
	}

	private char[] dealDictionary(IDictionary dict, char[] pattern)
	{
		VocabularyEntry entry = dict.lookup(pattern);
		//FIXME: Stupid test
		char[] ret = 
			entry.word ~ " - " ~ entry.phonetics ~ "\n" ~
			entry.translation;
		return ret;
	}

	public char[] query(char[] pattern)
	{
		char[] result;
		foreach(dict; m_dictman.dictionaries)
		{
			result ~= dealDictionary(dict, pattern);
		}
		return result;
	}

}
